package com.haotunet.app.goldtreasure.activity;

import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.haotunet.app.goldtreasure.R;
import com.haotunet.app.goldtreasure.db.DBOpenHelper;
import com.haotunet.app.goldtreasure.model.BaseModel;
import com.haotunet.app.goldtreasure.model.UserInfo;
import com.haotunet.app.goldtreasure.net.ActionUtil;
import com.haotunet.app.goldtreasure.other.Interface;
import com.haotunet.app.goldtreasure.utils.Config;
import com.haotunet.app.goldtreasure.utils.ShareStore;
import com.haotunet.app.goldtreasure.utils.Strs;
import com.haotunet.app.goldtreasure.utils.UIHelper;
import com.haotunet.app.goldtreasure.views.TitleView;
import com.umeng.message.UmengRegistrar;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
import cn.tongdun.android.shell.FMAgent;

public class WeChatRegisterActivity extends BaseActivity {

    @InjectView(R.id.title_view)
    TitleView titleView;
    @InjectView(R.id.et_login_pwd)
    EditText etLoginPwd;
    private String wechatCode;
    private String phone;
    private ShareStore mStore;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_we_chat_register);
        mStore = new ShareStore(this);
        ButterKnife.inject(this);
        initView();
    }

    private void initView() {
        Intent intent = getIntent();
        wechatCode = intent.getStringExtra("wechatCode");
        phone = intent.getStringExtra("phone");
        titleView.getTitletextView().setVisibility(View.VISIBLE);
        titleView.getTitletextView().setText("手机验证");
        titleView.getBackView().setVisibility(View.VISIBLE);
        titleView.getBackView().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WeChatRegisterActivity.this.finish();
            }
        });
    }

    @OnClick(R.id.btn_complete)
    public void onClick() {
        String password = etLoginPwd.getText().toString().trim();
        wechatRegister(password);
    }

    private void wechatRegister(String password) {
        String device_token = UmengRegistrar.getRegistrationId(WeChatRegisterActivity.this);
        String imei = Config.getImei(WeChatRegisterActivity.this);
        String blackBox = FMAgent.onEvent(this);
        String channelId = Config.getStringMetaData(WeChatRegisterActivity.this, "UMENG_CHANNEL");
        ActionUtil actionUtil = new ActionUtil(WeChatRegisterActivity.this);
        actionUtil.wechatRegister(wechatCode, phone, password, device_token, imei, blackBox, channelId);
        actionUtil.setOnPassBackListener(new Interface.OnPassBackListener() {
            @Override
            public void passBack(BaseModel model) {
                if (isFinishing()){
                    return;
                }
                UserInfo info = (UserInfo) model;
                if (info == null) {
                    UIHelper.showToast(WeChatRegisterActivity.this, getString(R.string.rank_fail));
                    return;
                }
                if (info.getErrorMsg() != null) {
                    UIHelper.showToast(WeChatRegisterActivity.this, info.getErrorMsg());
                } else {
                    storeUserInfo(info);
                    Intent intent = new Intent();
                    intent.setClass(WeChatRegisterActivity.this, MainActivity.class);
                    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                    intent.putExtra("type", "wechatLogin");
                    startActivity(intent);
                    WeChatRegisterActivity.this.finish();
                }
            }
        });
    }

    private void storeUserInfo(UserInfo info) {
        mStore.putBoolean(Strs.IS_LOGIN, true);
        mStore.putInt(Strs.IS_HAD_TRADING_PWD, info.getIsHadTradingPassword());
        mStore.putInt(Strs.IS_HAD_BANK_CARD, info.getIsHadBankCard());
        mStore.commit();
        DBOpenHelper helper = new DBOpenHelper(WeChatRegisterActivity.this);
        SQLiteDatabase db = helper.getReadableDatabase();
        String mobile = info.getUserName();
        String sql = "select * from HJB_USER where mobile = " + "'" + mobile + "'";
        Cursor rawQuery = db.rawQuery(sql, null);
        if (rawQuery.getCount() != 0) {
            helper.updateUser(mobile, "0", info);
        } else {
            helper.insertUser("0", info);
        }
    }
}
